<?php

/**
 * This is the model class for table "models".
 *
 * The followings are the available columns in table 'models':
 * @property integer $row_id
 * @property integer $cat_id
 * @property string $cat_title
 * @property integer $brand_id
 * @property string $brand_title
 * @property string $model_id
 * @property string $model_title
 * @property string $model_image
 * @property string $model_rating
 * @property string $model_price
 * @property string $model_popular
 * @property string $model_yandex_id
 * @property string $model_new_title
 * @property integer $is_active
 * @property string $is_parent
 * @property string $model_test_title
 * @property string $model_status
 */
class NewModels extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'models';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('cat_id, brand_id, is_active', 'numerical', 'integerOnly' => true),
            array('cat_title, brand_title, model_title, model_image, model_rating, model_popular, model_new_title, is_parent, model_test_title', 'length', 'max' => 255),
            array('model_id, model_price, model_yandex_id, model_status', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('row_id, cat_id, cat_title, brand_id, brand_title, model_id, model_title, model_image, model_rating, model_price, model_popular, model_yandex_id, model_new_title, is_active, is_parent, model_test_title, model_status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'row_id' => 'Row',
            'cat_id' => 'Cat',
            'cat_title' => 'Категория',
            'brand_id' => 'Brand',
            'brand_title' => 'Бренд',
            'model_id' => 'Model',
            'model_title' => 'Модель',
            'model_image' => 'Model Image',
            'model_rating' => 'Model Rating',
            'model_price' => 'Цена',
            'model_popular' => 'Model Popular',
            'model_yandex_id' => 'Яндекс айди',
            'model_new_title' => 'Model New Title',
            'is_active' => 'Is Active',
            'is_parent' => 'Is Parent',
            'model_test_title' => 'Model Test Title',
            'model_status' => 'Model Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('row_id', $this->row_id);
        $criteria->compare('cat_id', $this->cat_id);
        $criteria->compare('cat_title', $this->cat_title, true);
        $criteria->compare('brand_id', $this->brand_id);
        $criteria->compare('brand_title', $this->brand_title, true);
        $criteria->compare('model_id', $this->model_id);
        $criteria->compare('model_title', $this->model_title, true);
        $criteria->compare('model_image', $this->model_image, true);
        $criteria->compare('model_rating', $this->model_rating);
        $criteria->compare('model_price', $this->model_price);
        $criteria->compare('model_popular', $this->model_popular, true);
        $criteria->compare('model_yandex_id', $this->model_yandex_id);
        $criteria->compare('model_new_title', $this->model_new_title, true);
//        $criteria->compare('is_active', '!=0');
        //$criteria->addCondition('is_active != 0');
        $criteria->compare('is_parent', $this->is_parent, true);
        $criteria->compare('model_test_title', $this->model_test_title, true);
        $criteria->compare('model_status', $this->model_status, true);
        $criteria->order = '"model_status"';
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination' => array(
                'pageSize' => 30,
                'pageVar' => 'page'
            ),
        ));
    }

    public function getStatusText() {
        $status = array('added' => 'Добавлена на сайт', 'rejected' => 'Отклонена', 'new' => 'Новая', 'linked' => 'Прикреплена к осн. модели');
        return isset($status[$this->model_status]) ? $status[$this->model_status] : '';
    }

    public function getClass() {
        return $this->model_status;
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return NewModels the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
